import 'package:flutter/cupertino.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:rxdart/rxdart.dart';
import 'package:schoollifedemo/base/common_function.dart';
import 'package:schoollifedemo/base/routers.dart';
import 'package:schoollifedemo/base/url_contract.dart';
import 'package:schoollifedemo/commonwidget/common_base_viewmodel.dart';
import 'package:schoollifedemo/entity/Entity.dart';

class AnnouncementSeeMorePageViewModel extends BaseViewModel {
  SchoolAnnouncementResponse? _schoolAnnouncementResponse;

  SchoolAnnouncementResponse? get schoolAnnouncementResp =>
      _schoolAnnouncementResponse;
  SchoolAnnouncementResponse? _filterResponse;

  SchoolAnnouncementResponse? get filterResp => _filterResponse;

  PublishSubject schoolAnnouncementLoadingState =
      PublishSubject<CommonLoadingState?>();

  // 拉取学校信息
  Future<void> pullSchoolAnnouncement({
    OnSuccess? onSuccess,
    OnFailure? onFailure,
  }) async {
    final params = <String, Object?>{};
    params['account'] = getApplication().getLoginAccount() ?? '';
    final result = await getNetworkUtil()
        .postHttp(UrlContract.pullUserSchoolAnnouncement, params: params);
    final resp = SchoolAnnouncementResponse.fromMap(result);
    if (resp.status == 0) {
      _schoolAnnouncementResponse = resp;
      _filterResponse = SchoolAnnouncementResponse.fromMap(result);
      if (onSuccess != null) {
        onSuccess();
      }
    } else {
      schoolAnnouncementLoadingState.add(CommonLoadingState.failure);
      if (onFailure != null) {
        onFailure(resp.msg);
      }
    }
  }

  // 筛选标题
  Future<void> flutterTitle({String? mainTitleMd5}) async {
    if (mainTitleMd5 != null && mainTitleMd5.isNotEmpty == true) {
      _filterResponse?.announcementList?.clear();

      for (int index = 0;
          index < (_schoolAnnouncementResponse?.announcementList?.length ?? 0);
          index++) {
        final convertMainTitle = await getChannelService()
            .getCommonChannelFunc()
            ?.convertMD5(_schoolAnnouncementResponse
                    ?.announcementList?[index]?.mainTitle ??
                '');
        if (convertMainTitle == mainTitleMd5) {
          _filterResponse?.announcementList
              ?.add(_schoolAnnouncementResponse?.announcementList?[index]);
        }
      }
    }
    schoolAnnouncementLoadingState.add(CommonLoadingState.successTypeOne);
  }

  // 转到详情页面
  Future<void> turnToDetailPage(
    BuildContext context, {
    String? url,
    String? mainTitle,
  }) async {
    final params = <String, Object?>{};
    if (url?.isNotEmpty == true) {
      params['base64_url'] = await getChannelService()
          .getCommonChannelFunc()
          ?.encodeBase64(url ?? '');
    }
    if (url?.isNotEmpty == true) {
      params['base64_main_title'] = await getChannelService()
          .getCommonChannelFunc()
          ?.encodeBase64(mainTitle ?? '');
    }
    return await getApplication().routerNavigateTo(
      context,
      RoutersPath.seeSingleAnnouncementDetailPage,
      params: params,
    );
  }

  Future<void> backPage(BuildContext context) async {
    return getApplication().routerPop(context);
  }
}
